package com.itany.netclass.service.proxy;

import com.itany.netclass.entity.CourseType;
import com.itany.netclass.entity.User;
import com.itany.netclass.exception.*;
import com.itany.netclass.factory.ObjectFactory;
import com.itany.netclass.service.CourseTypeService;
import com.itany.netclass.tran.TransactionManager;

import java.util.List;

/**
 * Author:sjy@itany.com
 * Date:21-10-14 上午9:10
 * Version:1.0-SNAPSHOT
 * Description:
 */
public class CourseTypeServiceProxy implements CourseTypeService {
    @Override
    public List<CourseType> findAllCourseType(User user, String parentId) throws ServiceException, NoLoginException {
        TransactionManager tran= (TransactionManager) ObjectFactory.getObject("tran");
        CourseTypeService courseTypeService= (CourseTypeService) ObjectFactory.getObject("CourseTypeServiceTarget");
        try {
            tran.commit();
            List<CourseType> type = courseTypeService.findAllCourseType(user,parentId);
            tran.commit();
            return type;
        } catch (NoLoginException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }

    }

    @Override
    public CourseType findCourseTypeById(String id) throws ServiceException {
        TransactionManager tran= (TransactionManager) ObjectFactory.getObject("tran");
        CourseTypeService courseTypeService= (CourseTypeService) ObjectFactory.getObject("CourseTypeServiceTarget");
        try {
            tran.begin();
            CourseType course = courseTypeService.findCourseTypeById(id);
            tran.commit();
            return course;
        } catch (ServiceException e) {
            throw e;
        }
    }

    @Override
    public void addCourseType(String typeName, String parentId) throws ServiceException, NullSringException, TypeExistException {
        TransactionManager tran= (TransactionManager) ObjectFactory.getObject("tran");
        CourseTypeService courseTypeService= (CourseTypeService) ObjectFactory.getObject("CourseTypeServiceTarget");
        try {
            tran.begin();
            courseTypeService.addCourseType(typeName,parentId);
            tran.commit();
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        } catch (TypeExistException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public List<CourseType> backCourseType(String id) throws ServiceException, NoFatherException {
        TransactionManager tran= (TransactionManager) ObjectFactory.getObject("tran");
        CourseTypeService courseTypeService= (CourseTypeService) ObjectFactory.getObject("CourseTypeServiceTarget");
        try {
            tran.begin();
            List<CourseType> list = courseTypeService.backCourseType(id);
            tran.commit();
            return list;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (NoFatherException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void modifyCourseTypeById(String id, String typeName ,String parentId) throws ServiceException, NullSringException, TypeExistException {
        TransactionManager tran= (TransactionManager) ObjectFactory.getObject("tran");
        CourseTypeService courseTypeService= (CourseTypeService) ObjectFactory.getObject("CourseTypeServiceTarget");
        try {
            tran.begin();
            courseTypeService.modifyCourseTypeById(id,typeName,parentId);
            tran.commit();
        } catch (ServiceException e) {
            throw e;
        } catch (NullSringException e) {
            throw e;
        } catch (TypeExistException e) {
            throw e;
        }
    }

    @Override
    public void yongCourseTypeById(String id,String status) throws Exception{
        TransactionManager tran= (TransactionManager) ObjectFactory.getObject("tran");
        CourseTypeService courseTypeService= (CourseTypeService) ObjectFactory.getObject("CourseTypeServiceTarget");
        try {
            tran.begin();
            courseTypeService.yongCourseTypeById(id,status);
            tran.commit();
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (FatherJinyongException e) {
            tran.rollback();
            throw e;
        }catch (Exception e){
            tran.rollback();
            throw e;
        }
    }
}
